package pers.karl.export.util;

import lombok.extern.slf4j.Slf4j;

import java.sql.*;

@Slf4j
public class OracleUtils {

    /**
     * 默认端口号
     */
    public static final int DEFAULT_PORT = 1521;

    /**
     * 默认服务名
     */
    public static final String DEFAULT_SERVICE = "ORCL";

    static {
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
        } catch (ClassNotFoundException e) {
            log.error("oracle driver fail", e);
        }
    }

    /**
     * 连接默认数据库、默认端口
     * @param ip
     * @param user
     * @param password
     * @return
     * @throws SQLException
     */
    public static Connection getConnection(String ip, String user, String password) throws SQLException {
        return getConnection(ip, DEFAULT_PORT, DEFAULT_SERVICE, user, password);
    }

    /**
     * 获取连接
     *
     * @param ip
     * @param port
     * @param serviceId
     * @param user
     * @param password
     * @return
     */
    public static Connection getConnection(String ip, int port, String serviceId, String user, String password) throws SQLException {
        String url = String.format("jdbc:oracle:thin:@%s:%d/%s", ip, port, serviceId);
        return SqlUtils.getConnection(url, user, password);
    }

}
